package com.android.qjpmc.application_module.home.fragment;

import android.os.Handler;
import android.view.View;

import com.android.qjpmc.R;
import com.android.qjpmc.bean.SiteStationBean;
import com.android.qjpmc.constant.Constant;
import com.android.qjpmc.data_module.CUser;
import com.android.qjpmc.data_module.CUserModule;
import com.android.qjpmc.data_module.PwrDataModule;
import com.android.qjpmc.data_module.PwrDevice;
import com.android.qjpmc.data_module.PwrStation;
import com.android.qjpmc.databinding.FragmentStationListBinding;
import com.android.qjpmc.databinding.ItemStationBinding;
import com.android.qjpmc.databinding.ItemStationDeviceBinding;
import com.android.qjpmc.manager.IntentManager;
import com.android.qjpmc.application_module.home.contract.HomePageContract;
import com.android.qjpmc.application_module.home.viewModel.HomePageViewModel;
import com.android.qjpmc.widget.databindingadapter.BaseBindingItemPresenter;
import com.android.qjpmc.widget.databindingadapter.BaseDataBindingDecorator;
import com.android.qjpmc.widget.databindingadapter.SingleTypeBindingAdapter;
import com.android.qjpmc.widget.mvvm.factory.CreateViewModel;
import com.android.qjpmc.widget.mvvm.view.BaseMVVMFragment;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

@CreateViewModel(HomePageViewModel.class)
public class StationListFragment extends BaseMVVMFragment<HomePageViewModel, FragmentStationListBinding> implements HomePageContract.View, BaseBindingItemPresenter {
    private List<SiteStationBean> stationList;

    private Handler m_handler = null;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_station_list;
    }

    ArrayList<PwrStation> pwrStations = null;
    ArrayList<PwrDevice> m_PwrDeviceSet = null;

    PwrStation GetStation(int StationListIndex) {
        if (pwrStations == null) {
            return null;
        }
        return pwrStations.get(StationListIndex);
    }

    @Override
    public void initView() {

        mBinding.setView(this);
        pwrStations = PwrDataModule.getInstance().GetAllStationList();

        CUserModule user_module = PwrDataModule.getInstance().GetUserModule();
        if (user_module == null) {
            mBinding.loginUserName.setText("无用户");
        } else {
            mBinding.loginUserName.setText(user_module.GetCurrentUserName());
        }


    }

    @Override
    public void initData() {
        //while (PwrDataModule.getInstance().IsStationDownLoad() != 1)
        {

        }
        PwrDataModule.getInstance().SetStationListFragement(this);
        m_handler = new Handler();
        mViewModel.getStationList();
    }

    @Override
    public void initEvent() {

    }

    //个人信息
    @Override
    public void onMyData() {
        IntentManager.toUserInformationActivity(mActivity);
    }

    //开发人员调试
    @Override
    public void onDeveloperDebugging() {
        IntentManager.toDeveloperDebuggingActivity(mActivity);
    }

    //变电站
    @Override
    public void onTransformerSubstation(String stationId) {
        IntentManager.toStationActivity(mActivity, stationId);
    }

    @Override
    public void returnSiteStationData(List<SiteStationBean> siteStationBeans) {
        List<Integer> images = new ArrayList<>();
        images.add(R.drawable.pwr_device);
        images.add(R.drawable.ic_action_name_2);
        images.add(R.drawable.ic_action_name_3);
        images.add(R.drawable.ic_action_name_4);
        Logger.d("returnSiteStationData" + siteStationBeans.toString());
        //  if (1 == 1)
        // {
        this.stationList = siteStationBeans;
        SingleTypeBindingAdapter adapter = new SingleTypeBindingAdapter(mActivity, stationList, R.layout.item_station);
        mBinding.recyclerView.setAdapter(adapter);
        adapter.setItemPresenter(this);
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter.setItemDecorator(new BaseDataBindingDecorator<SiteStationBean, ItemStationBinding>() {
            @Override
            public void decorator(ItemStationBinding binding, int position, int viewType, SiteStationBean stationBean) {
                //  ArrayList<PwrDevice> pwrDevices = PwrDataModule.getInstance().GetDeviceList(data.getM_ID());
                binding.tvDeviceNumber.setText("机柜数量：无字段");
                SingleTypeBindingAdapter devicesAdapter = new SingleTypeBindingAdapter(getContext(), images, R.layout.item_station_device);
                binding.recyclerView.setAdapter(devicesAdapter);
                GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4, LinearLayoutManager.VERTICAL, false);
                binding.recyclerView.setLayoutManager(gridLayoutManager);
                devicesAdapter.setItemPresenter(StationListFragment.this);
                devicesAdapter.setItemDecorator(new BaseDataBindingDecorator<Integer, ItemStationDeviceBinding>() {
                    @Override
                    public void decorator(ItemStationDeviceBinding Devicebinding, int device_index, int viewType, Integer data) {
                        Devicebinding.ivDevice.setImageResource(images.get(0));
                        Devicebinding.tvDevice.setText("机柜 : " + (device_index + 1));
                        Devicebinding.llItemDevice.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {                              /*  PwrStation Station = GetStation(position);
                                PwrDataModule.getInstance().SetCurrentStationNode(Station);
                                if (Station != null) {
                                    PwrDevice ThePwrDevice = Station.GetPwrDeviceByIndex(device_index);
                                    //PwrDataModule.getInstance().
                                    if (ThePwrDevice != null) {
                                        Station.SetCurrentPwrDeviceNode(ThePwrDevice);
                                        onTransformerSubstation("1");
                                    }
                                }*/
                                onTransformerSubstation(stationBean.company_id);
                            }
                        });
                    }
                });

            }
        });
        //}

    }


    public void onNotice() {
        IntentManager.toApprovalTicketActivity(mActivity);
    }

    @Override
    public void onItemClick(int position, Object itemData) {
        SiteStationBean siteStationBean = stationList.get(position);
        if (siteStationBean == null) {
            ;
        } else {
            PwrStation Station = PwrDataModule.getInstance().GetPwrStationByID(Integer.parseInt(siteStationBean.company_id));
            PwrDataModule.getInstance().SetCurrentStationNode(Station);
            onTransformerSubstation(siteStationBean.company_id);
        }
      /*  PwrStation Station = GetStation(position);
        PwrDataModule.getInstance().SetCurrentStationNode(Station);
        if (Station != null) {
            onTransformerSubstation(siteStationBean.stationId);
        }*/
    }

    Runnable m_UpdateUIRunnable = new Runnable() {
        @Override
        public void run() {
            returnSiteStationData(PwrDataModule.getInstance().GetStaionBeanList());
        }
    };

    public int UpdateStationListUI() {
        if (m_handler == null) {
            return Constant.GEN_ERROR;
        }
        m_handler.post(m_UpdateUIRunnable);
        return Constant.NO_ERROR;
    }
}
